package cn.itcast.bos.service.base.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.base.CourierRepository;
import cn.itcast.bos.dao.base.FixedAreaRepository;
import cn.itcast.bos.dao.base.TakeTimeRepository;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.domain.base.TakeTime;
import cn.itcast.bos.service.base.IFixedAreaService;
@Service
@Transactional
public class FixedAreaServiceImpl implements IFixedAreaService {
	
	@Autowired
	private FixedAreaRepository fixedAreaRepository;
	
	@Override
	public void save(FixedArea model) {
		fixedAreaRepository.save(model);
	}

	@Override
	public Page<FixedArea> findPageData(Specification<FixedArea> specification, Pageable pageable) {
		return fixedAreaRepository.findAll(specification, pageable);
	}

	@Override
	public void delete(String idStr) {
		fixedAreaRepository.delete(idStr);
		
	}
	
	@Autowired
	private CourierRepository courierRepository;
	@Autowired
	private TakeTimeRepository takeTimeRepository;
	
	@Override	//fixedArea对象中只含有id的值		疑问:模型驱动里的fixeeArea对象分装的不是有完整的数据吗,为什么现在只有id属性了	???
	public void associationCourierToFixedArea(FixedArea fixedArea, Integer courierId, Integer takeTimeId) {
		//根据id查找
		FixedArea presistFixedArea = fixedAreaRepository.findOne(fixedArea.getId());
		Courier courier=courierRepository.findOne(courierId);
		TakeTime takeTime=takeTimeRepository.findOne(takeTimeId);
		
		//快递员关联到定区上
		presistFixedArea.getCouriers().add(courier);
		//取派时间与快递员是一对多,所以将取派时间关联到快递员上
		courier.setTakeTime(takeTime);
	}

}
